<html>
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
</head>
<body>
<form class="layui-form" lay-filter="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label"><text title="必填项" style="color:red;">*</text>用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" lay-verify="required" autocomplete="off" placeholder="请输入用户名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><text title="必填项" style="color:red;">*</text>昵称</label>
                    <div class="layui-input-block">
                        <input type="text"  name="nickname" lay-verify="required" autocomplete="off" placeholder="请输入昵称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><text title="必填项" style="color:red;">*</text>密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="password" lay-verify="required" autocomplete="off" placeholder="请输入密码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><text title="必填项" style="color:red;">*</text>重复密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="re_password" lay-verify="required" autocomplete="off" placeholder="请输入重复密码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">账号状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="正常" checked>
                        <input type="radio" name="status" value="2" title="禁用">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">是否超管</label>
                    <div class="layui-input-block">
                        <input type="radio" name="is_super_manager" value="1" title="是">
                        <input type="radio" name="is_super_manager" value="2" title="否" checked>
                    </div>
                </div>
<!--                <div class="layui-form-item">-->
<!--                    <label class="layui-form-label">头像</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <div class="laytpUpload"-->
<!--                             data-name="avatar"-->
<!--                             data-accept="image"-->
<!--                             data-width="400"-->
<!--                        ></div>-->
<!--                    </div>-->
<!--                </div>-->
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <div class="xmSelect"
                             data-name="role_ids"
                             data-sourceType="route"
                             data-source="/admin.role/index"
                             data-textField="name"
                             data-valueField="id"
                             data-paging="false"
                             data-direction="up"
                        ></div>
                    </div>
                </div>
                <!-- 新增组织下拉选项 -->
                <div class="layui-form-item">
                    <label class="layui-form-label">组织</label>
                    <div class="layui-input-block">
                        <select name="org_id" lay-filter="organization">
                            <option value="">请选择组织</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="laytp-btn laytp-btn-primary laytp-btn-sm" lay-submit="" lay-filter="add">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="laytp-btn laytp-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    if(localStorage.getItem("staticDomain")){
        document.write("<link rel='stylesheet' href='" + localStorage.getItem("staticDomain") + "/component/laytp/css/laytp.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/layui/layui.js?v=" + Date.now() + "'><\/script>");
        document.write("<script src='" + localStorage.getItem("staticDomain") + "/component/laytp/layuiConfig.js?v=" + Date.now() + "'><\/script>");
    }else{
        document.write("<link rel='stylesheet' href='/static/component/laytp/css/laytp.css?v=" + localStorage.getItem("version") + "'>");
        document.write("<script src='/static/component/layui/layui.js?v=" + Date.now() + "'><\/script>");
        document.write("<script src='/static/component/laytp/layuiConfig.js?v=" + Date.now() + "'><\/script>");
    }
</script>
<script>
    layui.use(['laytp'], function () {
        const $ = layui.$,
            facade = layui.facade;

        // 动态加载组织数据
        facade.ajax({
            route: '/admin.org/index',
            successAlert: false
        }).done(function (res) {
            if (res.code === 0) {
                let select = $('select[name="org_id"]');
                res.data.forEach(function (item) {
                    select.append(`<option value="${item.id}">${item.name}</option>`);
                });
                layui.form.render('select'); // 刷新下拉框样式
            }
        });

        layui.form.on('submit(add)', function (data) {
            var btnAnim = layui.button.load({ elem: $(this) });
            facade.ajax({
                route: '/admin.user/add',
                data: data.field
            }).done(function (res) {
                if (res.code === 0) {
                    parent.layui.layer.close(parent.layui.layer.getFrameIndex(window.name)); // 关闭当前页
                    parent.layui.table.reloadData("laytp-table");
                }
                btnAnim.stop();
            }).fail(function () {
                btnAnim.stop();
            });
            return false;
        });
    });
</script>
</body>
</html>